using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using UniversalAdmin.Application.Commands;
using UniversalAdmin.Application.Dto;
using UniversalAdmin.Application.Services;
using UniversalAdmin.Shared.Enums;
using UniversalAdmin.Api.Filters;

[ApiController]
[Route("api/permissions")]
[Authorize]
public class PermissionsController : ControllerBase
{
    private readonly IAppPermissionService _permissionService;
    public PermissionsController(IAppPermissionService permissionService) { _permissionService = permissionService; }

            [HttpGet]
        [RequirePermission("permission.view")]
        public async Task<ApiResult> Get([FromQuery] int? page = null, [FromQuery] int? size = null, [FromQuery] string? search = null)
    {
        if (page.HasValue && size.HasValue)
        {
            var result = await _permissionService.GetPagedAsync(new PagedQuery { Page = page.Value, Size = size.Value, Search = search });
            return new ApiResult { Code = result.Code, Message = result.Message, Data = result.Data, Timestamp = result.Timestamp };
        }
        else
        {
            var result = await _permissionService.GetAllAsync(search);
            return new ApiResult { Code = result.Code, Message = result.Message, Data = result.Data, Timestamp = result.Timestamp };
        }
    }

            [HttpGet("{id}")]
        [RequirePermission("permission.view")]
        public async Task<ApiResult<PermissionDto>> GetById(Guid id)
            => await _permissionService.GetByIdAsync(id);

            [HttpPost]
        [RequirePermission("permission.create")]
        public async Task<ApiResult<PermissionDto>> Create([FromBody] CreatePermissionDto dto)
            => await _permissionService.CreateAsync(dto);

    [HttpPut("{id}")]
    [RequirePermission("permission.edit")] // 需要编辑权限权限
    public async Task<ApiResult<PermissionDto>> Update(Guid id, [FromBody] UpdatePermissionDto dto)
        => await _permissionService.UpdateAsync(id, dto);

    [HttpDelete("{id}")]
    [RequirePermission("permission.delete")] // 需要删除权限权限
    public async Task<ApiResult> Delete(Guid id)
        => await _permissionService.DeleteAsync(id);

    [HttpPut("{id}/enable")]
    [RequirePermission("permission.edit")] // 需要编辑权限权限
    public async Task<ApiResult> Enable(Guid id)
        => await _permissionService.EnableAsync(id);

    [HttpPut("{id}/disable")]
    [RequirePermission("permission.edit")] // 需要编辑权限权限
    public async Task<ApiResult> Disable(Guid id)
        => await _permissionService.DisableAsync(id);
} 